﻿'   FAHWatch7   
'
'   Copyright (c) 2011 Marvin Westmaas ( MtM / Marvin_The_Martian )
'
'   This program is free software: you can redistribute it and/or modify
'   it under the terms of the GNU General Public License as published by
'   the Free Software Foundation, either version 3 of the License, or
'   (at your option) any later version.
'
'   This program is distributed in the hope that it will be useful,
'   but WITHOUT ANY WARRANTY; without even the implied warranty of
'   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'   GNU General Public License for more details.
'
'   You should have received a copy of the GNU General Public License
'   along with this program.  If not, see <http://www.gnu.org/licenses/>.
Imports ZedGraph
Public Class frmZGRAPH
    Public bAllowclose As Boolean = False
    Public myWU As New clsWU
    Private myHistoryForm As frmHistory
    Public Sub ShowWUFrames(WU As clsWU, Optional HistoryForm As frmHistory = Nothing)
        Try
            If Not IsNothing(HistoryForm) Then myHistoryForm = HistoryForm
            myWU = WU
            Me.Text = WU.PRCG & " " & WU.dtDownloaded.ToString & vbTab & " topmost=" & Me.TopMost.ToString
            Dim ppProject As New ZedGraph.PointPairList
            For xInt As Int32 = 0 To WU.Frames.Count - 2
                Console.WriteLine(xInt)
                Dim tsFrame As TimeSpan = WU.Frames(xInt + 1).FrameDT - WU.Frames(xInt).FrameDT
                ppProject.Add(New XDate(WU.Frames(xInt).FrameDT), tsFrame.TotalDays)
            Next
            zgProject.MasterPane.PaneList.Clear()
            Dim mPane As New GraphPane
            mPane.AddCurve("", ppProject, Color.Blue)
            mPane.XAxis.Type = ZedGraph.AxisType.Date
            mPane.YAxis.Type = AxisType.Date
            mPane.Rect = Me.ClientRectangle

            zgProject.GraphPane = mPane
            zgProject.AxisChange()
            zgProject.Refresh()
        Catch ex As Exception
            WriteError(ex.Message, Err)
        End Try
    End Sub
    Private Sub zgProject_MouseDoubleClick(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles zgProject.MouseDoubleClick
        Try
            Me.TopMost = Not Me.TopMost
            Me.Text = myWU.PRCG & " " & myWU.dtDownloaded.ToString & vbTab & " topmost=" & Me.TopMost.ToString
        Catch ex As Exception
            WriteError(ex.Message, Err)
        End Try
    End Sub
    Public Sub CloseForm()
        Try
            bAllowclose = True
            Me.Close()
        Catch ex As Exception
            WriteError(ex.Message, Err)
        End Try
    End Sub
    Private Sub frmZGRAPH_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Try
            If Not bAllowclose Then
                e.Cancel = True
                Me.Hide()
            End If
        Catch ex As Exception
            WriteError(ex.Message, Err)
        End Try
    End Sub

    Private Sub frmZGRAPH_KeyUp(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
        Try
            If Not IsNothing(myHistoryForm) Then
                'Allow going through the wu list with left and right
                If e.KeyData = Keys.Right Or e.KeyData = Keys.Left Then
                    If e.KeyData = Keys.Left And myHistoryForm.lvWU.SelectedIndices(0) > 1 Then
                        myHistoryForm.lvWU.Items(myHistoryForm.lvWU.SelectedIndices(0) - 1).Selected = True
                    ElseIf e.KeyData = Keys.Right And myHistoryForm.lvWU.SelectedIndices(0) < myHistoryForm.lvWU.Items.Count - 1 Then
                        myHistoryForm.lvWU.Items(myHistoryForm.lvWU.SelectedIndices(0) + 1).Selected = True
                    End If
                End If
            End If
        Catch ex As Exception
            WriteError(ex.Message, Err)
        End Try
    End Sub

    Private Sub frmZGRAPH_PreviewKeyDown(sender As Object, e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles Me.PreviewKeyDown
        Try
            If e.KeyCode = Keys.Left Or e.KeyCode = Keys.Right Then e.IsInputKey = True
        Catch ex As Exception
            WriteError(ex.Message, Err)
        End Try
    End Sub

    Private Sub CloseFormToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles CloseFormToolStripMenuItem.Click
        Try
            Me.Hide()
        Catch ex As Exception
            WriteError(ex.Message, Err)
        End Try
    End Sub
    Private Sub TopMostToolStripMenuItem_CheckedChanged(sender As Object, e As System.EventArgs) Handles TopMostToolStripMenuItem.CheckedChanged
        Try
            Me.TopMost = TopMostToolStripMenuItem.Checked
        Catch ex As Exception
            WriteError(ex.Message, Err)
        End Try
    End Sub
#Region "Set form state"
    Public Delegate Sub dWindowState(fState As FormWindowState)
    Public Sub SetWindowState(State As FormWindowState)
        If Me.InvokeRequired Then
            Dim nI As New dWindowState(AddressOf SetWindowState)
            nI.Invoke([State])
        Else
            Me.WindowState = State
        End If
    End Sub
    Public Delegate Sub dSetEnabled(Enabled As Boolean)
    Public Sub SetEnabled(Enabled As Boolean)
        If Me.InvokeRequired Then
            Dim nI As New dSetEnabled(AddressOf SetEnabled)
            nI.Invoke([Enabled])
        Else
            Me.Enabled = Enabled
        End If
    End Sub
#End Region
End Class